﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using CNVP.Config;
using CNVP.Framework.Helper;
using System.Data;

namespace CNVP.JShop.DAL
{
    public class CNVP_DeliveryFreight
    {
        private string Prefix = DBConfig.Prefix;
        #region DAL
        public int Insert(Model.CNVP_DeliveryFreight obj)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("insert into  " + Prefix + "DeliveryFreight ([DeliveryFreightColumnID],[DeliveryType],[ProvinceArrayName],[ProvinceArray],[FirstPiece],[FirstFreight],[AddPiece],[AddFreight],[IsDefault])");
            sql.Append("values(@DeliveryFreightColumnID, @DeliveryType,@ProvinceArrayName, @ProvinceArray, @FirstPiece, @FirstFreight, @AddPiece, @AddFreight, @IsDefault)");
            sql.Append(";SELECT @@IDENTITY");
            SqlParameter[] pra = {
                            new SqlParameter("@DeliveryFreightColumnID",obj.DeliveryFreightColumnID),
							new SqlParameter("@DeliveryType",obj.DeliveryType),
                            new SqlParameter("@ProvinceArrayName",obj.ProvinceArrayName),
							new SqlParameter("@ProvinceArray",obj.ProvinceArray),
							new SqlParameter("@FirstPiece",obj.FirstPiece),
                            new SqlParameter("@FirstFreight",obj.FirstFreight),
                            new SqlParameter("@AddPiece",obj.AddPiece),
							new SqlParameter("@AddFreight",obj.AddFreight),
							new SqlParameter("@IsDefault",obj.IsDefault)
                                 };
            return Convert.ToInt32(DbHelper.ExecuteScalar(sql.ToString(), pra));
        }

        public int Delete(Model.CNVP_DeliveryFreight obj)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("delete from " + Prefix + "DeliveryFreight where [DeliveryFreightColumnID] = @DeliveryFreightColumnID");
            SqlParameter[] pra = {
                                new SqlParameter("@DeliveryFreightColumnID",obj.DeliveryFreightColumnID)
                                };
            return DbHelper.ExecuteNonQuery(sql.ToString(), pra);
        }

        public List<Model.CNVP_DeliveryFreight> LoadAllByDeliveryFreightColumnID(int DeliveryFreightColumnID, byte DeliveryType,string OrderBy)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select [ID],[ProvinceArrayName],[ProvinceArray],[FirstPiece],[FirstFreight],[AddPiece],[AddFreight],[IsDefault] from " + Prefix + "DeliveryFreight");
            sql.Append(" where [DeliveryFreightColumnID] = @DeliveryFreightColumnID and [DeliveryType] = @DeliveryType " + OrderBy + "");//注意必须要order by IsDefault
            SqlParameter[] pra = {
                                 new SqlParameter("@DeliveryFreightColumnID",DeliveryFreightColumnID),
						        new SqlParameter("@DeliveryType",DeliveryType)
                                    };
            DataTable DT = DbHelper.ExecuteTable(sql.ToString(),pra);
            List<Model.CNVP_DeliveryFreight> list = new List<Model.CNVP_DeliveryFreight>();
            if (DT != null && DT.Rows.Count > 0)
            {
                foreach (DataRow dr in DT.Rows)
                {
                    Model.CNVP_DeliveryFreight deliveryfreight = new Model.CNVP_DeliveryFreight();
                    deliveryfreight.ID = dr["ID"] is DBNull ? 0 : (int)dr["ID"];
                    deliveryfreight.ProvinceArrayName = dr["ProvinceArrayName"] is DBNull ? String.Empty : (string)dr["ProvinceArrayName"];
                    deliveryfreight.ProvinceArray = dr["ProvinceArray"] is DBNull ? String.Empty : (string)dr["ProvinceArray"];
                    deliveryfreight.FirstPiece = dr["FirstPiece"] is DBNull ? 0 : (int)dr["FirstPiece"];
                    deliveryfreight.FirstFreight = dr["FirstPiece"] is DBNull ? 0 : (decimal)dr["FirstFreight"];
                    deliveryfreight.AddPiece = dr["AddPiece"] is DBNull ? 0 : (int)dr["AddPiece"];
                    deliveryfreight.AddFreight = dr["AddFreight"] is DBNull ? 0 : (decimal)dr["AddFreight"];
                    deliveryfreight.IsDefault = dr["IsDefault"] is DBNull ? (byte)0 : (byte)dr["IsDefault"];
                    list.Add(deliveryfreight);
                }
            }
            return list;

        }

        #endregion
    }
}
